// subcontract/Business/businessDetails/index.js
Page({

  /**
   * 页面的初始数据
   */
  data: {
    aliyunServrBaseURL: getApp().config.aliyunServrBaseURL,
    communityInfo: {},//详情信息
    swiperCurrent: 1,//轮播图下标
    showText: false,//折叠文字
    showShare: false,//更多面板弹框
    options: [],//更多状态
    overlayShow: false,//分享提示
    userinfo: getApp().userinfo,
    weChatPopup: null,//添加微信
  },
  addWeChat(event) {
    this.data.weChatPopup.opneWeChat();
  },
  /**关注取下关注 */
  async communityfollow(event) {
    console.log(event);
    let { communityInfo } = this.data
    let object = {
      follow_id: communityInfo.user_id,
      status: communityInfo.is_follow == 0 ? '1' : '0'
    }
    const { code, data } = await getApp().api.communityfollow(object)
    if (code == 1) {
      communityInfo.is_follow = communityInfo.is_follow == 0 ? '1' : '0'
      this.setData({
        communityInfo,
      })
    }
  },
  onClickHide() {
    let { overlayShow } = this.data
    this.setData({ overlayShow: !overlayShow })
  },
  async onSelect(event) {
    console.log(event);
    let { index, name } = event.detail
    let { id, overlayShow } = this.data
    if (index == 1) {
      this.setData({ overlayShow: !overlayShow })
    }
    if (name == '删除') {
      const ret = await wx.showModal({
        title: '提示',
        content: '您确定要删除嘛？',
      })
      console.log(ret);
      if (ret.confirm) {
        console.log('用户点击确定')
        let object = {
          id
        }
        const { code, data } = await getApp().api.delArticle(object)
        if (code == 1) {
          getApp().showToast('删除成功！')
          setTimeout(() => {
            wx.navigateBack()
          }, 2000)
        }
      } else if (ret.cancel) {
        console.log('用户点击取消')
      }
    }
  },
  openPopup() {
    this.setData({ showShare: !this.data.showShare })
  },
  previewImage(event) {
    let { index } = event.currentTarget.dataset
    let { communityInfo } = this.data
    let image = communityInfo.images.map(item => item.image)
    wx.previewImage({
      current: image[index],
      urls: image
    });
  },
  swiperChange(event) {
    this.setData({
      swiperCurrent: event.detail.current + 1
    })
  },
  toggleText(event) {
    let { showText } = this.data
    this.setData({
      showText: !showText
    });
  },
  async communityDetail(id) {
    let object = {
      id
    }
    const { code, data } = await getApp().api.communityDetail(object)
    if (code == 1) {

      this.setData({
        communityInfo: data,
        options: getApp().userinfo.id == data.user_id ? [
          { name: '微信', icon: 'wechat', openType: 'share' },
          { name: '朋友圈', icon: 'wechat-moments' },
          { name: '删除', icon: `${getApp().config.aliyunServrBaseURL}/applet/static/mine/del.png` },
        ] : [
            { name: '微信', icon: 'wechat', openType: 'share' },
            { name: '朋友圈', icon: 'wechat-moments' },
            { name: '添加微信', icon: `qrcode` },
            { name: '投诉', icon: `${getApp().config.aliyunServrBaseURL}/applet/static/mine/tc.png` },
          ]
      })
    }
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    this.setData({ id: options.id })
    this.communityDetail(options.id)
    const weChatPopup = this.selectComponent('#weChat-popup');
    if (weChatPopup) {
      this.setData({
        weChatPopup: weChatPopup
      })
    }
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {

  },

  /**
   * 用户点击右上角分享
   */
  //分享朋友圈
  onShareTimeline(res) {
    return {
      title: '朋友圈',//分享的标题
      imageUrl: '',//展示的图片，这里是本地路径的写法，也可以写http或https开头的图片路径
      query: 'from=shareTimeline',//页面打开的传参
    }
  },
  // 分享到好友
  onShareAppMessage(res) {
    return {
      title: '好友',//分享的标题
      imageUrl: '',//展示的图片，这里是本地路径的写法，也可以写http或https开头的图片路径
      path: '/pages/index/index',//点击链接后跳转的页面，可以带参数
    }
  },
})